// pages/package/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        checked: false,
        loading: true,
        page: 1,
        add_show: false,
        express_number: '',
        note: '',
        editor_id: '',
        theme_color:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.get_list()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (wx.getStorageSync('add')) {
            this.add_demo()
            wx.removeStorageSync('add')
        }
        this.getTabBar().init()
        this.setData({
          theme_color:app.dlc.gain('theme_color')
        })
    },
    onChange(event) {
        this.setData({
            active: event.detail.index,
            page: 1,
            list: [],
            select_status: false,
            checked: false
        });
        this.get_list()
    },
    ChangeSearch(e) {
        this.setData({
            value: e.detail,
        });
    },
    onSearch() {
        this.setData({
            page: 1,
            list: [],
        })
        this.get_list()
    },
    allChange(event) {
        let list = this.data.list;
        list = list.map(item => {
            return {
                ...item,
                checked: event.detail
            }
        })
        this.setData({
            checked: event.detail,
            list,
            select_status: event.detail
        });
        if (event.detail) {
            let arr = []
            list.map(item => {
                arr.push(item.id)
            })
            this.setData({
                delete_id: arr
            })
        } else {
            this.setData({
                delete_id: [],
            })
        }
    },
    get_list() {
        const that = this;
        that.setData({
            loading: true,
            checked: false
        })
        app.wxRequest('/api/Goods/index', {
            page: this.data.page,
            status: this.data.active == 0 ? 1 : 0,
            keywords: this.data.value
        }, function (res) {
            if (res.code == 200) {
                let list = res.data;
                if (list.length > 0) {
                    list = list.map(item => {
                        return {
                            ...item,
                            checked: false
                        }
                    })
                }
                if (that.data.page == 1) {
                    that.setData({
                        list: list
                    })
                } else {
                    that.setData({
                        list: that.data.list.concat(list)
                    })
                }
                if (list.length > 0 && list.length == 10) {
                    let page=that.data.page;
                    page++
                    that.setData({
                        page: page,
                    })
                } else {
                    that.setData({
                        loading: false
                    })
                }

            } else {
                app.toast(res.message)
            }
        })
    },
    get_index(e) {
        let index = e.currentTarget.dataset.index;
        let list = this.data.list;
        list[index].checked = !list[index].checked
        this.setData({
            list: list
        })
        let status = list.every(item => item.checked == true)
        this.setData({
            checked: status
        })
        let select_status = list.some(item => item.checked)
        this.setData({
            select_status
        })
    },
    add_demo() {
        this.setData({
            add_show: true,
            express_number: '',
            note: '',
            editor_id: '',
        })
    },
    onClose() {
        this.setData({
            add_show: false
        })
    },
    get_express_number(e) {
        this.setData({
            express_number: e.detail
        })
    },
    get_note(e) {
        this.setData({
            note: e.detail
        })
    },
    add_list() {
        const that = this;
        if (!this.data.express_number) {
            app.toast('请输入快递单号')
        } else if (!this.data.note) {
            app.toast('请输入快递物品名称')
        } else {
            let obj = {
                express_number: that.data.express_number,
                note: that.data.note
            }
            if (that.data.editor_id) {
                that.edotr_data()
            } else {
                app.wxRequest('/api/Goods/add', obj, function (res) {
                    if (res.code == 200) {
                        that.agin()
                        that.onClose()
                    } else {
                        app.toast(res.message)
                    }
                })
            }

        }
    },
    edotr_data() {
        const that = this;
        app.wxRequest('/api/Goods/update', {
            id: that.data.editor_id,
            note: that.data.note
        }, function (res) {
            if (res.code == 200) {
                that.setData({
                    editor_id: ''
                })
                that.agin()
                that.onClose()
            } else {
                app.toast(res.message)
            }
        })
    },
    agin() {
        this.setData({
            page: 1,
            list: [],
            delete_id: [],
            select_status: false,
            checked: false
        })
        this.get_list()
    },
    copy_num(e) {
        let index = e.currentTarget.dataset.index;
        let code = this.data.list[index].express_number;
        wx.setClipboardData({
            data: code,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        app.toast('复制成功')
                        // console.log(res.data) // data
                    }
                })
            }
        })
    },
    look_num(e) {
        let index = e.currentTarget.dataset.index;
        let code = this.data.list[index].express_number;
        wx.navigateTo({
            url: "plugin://kdPlugin/index?num=" + code + "&appName=集轻松"
        })
    },
    editor_name(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            express_number: this.data.list[index].express_number,
            note: this.data.list[index].note,
            editor_id: this.data.list[index].id,
            add_show: true
        })
    },
    delete_list_demo(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            delete_id: [this.data.list[index].id]
        })
        this.delete_list()
    },
    delete_list() {
        const that = this;
        wx.showModal({
            title: '提示',
            content: '确认删除包裹吗？',
            success(res) {
                if (res.confirm) {
                    app.wxRequest('/api/Goods/del', {
                        ids: that.data.delete_id
                    }, function (res) {
                        if (res.code == 200) {
                            app.toast('删除成功')
                            that.agin()
                        } else {
                            app.toast(res.message)
                        }
                    })
                }
            }
        })
    },
    add_delete() {
        const that = this;
        let list = that.data.list;
        let ids = []
        list.map(item => {
            if (item.checked) {
                ids.push(item.id)
            }
        })
        this.setData({
            delete_id: ids
        })
        this.delete_list()
    },
    place_an_order() {
        const that = this;
        let list = that.data.list;
        let arr = list.filter(item => {
            return item.checked
        })
        wx.setStorageSync('place_order', arr)
        wx.navigateTo({
            url: '/home/pages/place_an_order/index'
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        wx.stopPullDownRefresh();
        this.agin()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.loading) {
            this.get_list()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})